-- NewguildInfo
-- Author: mm
-- Date: 2018-03-19 15:44:36
-- 公会基础数据结构

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

function ClassRef:ctor(data)
	self._gid = ""
	self._gName = ""
	self._gLv = 0
	self._gExp = 0
	self._gMemberNum = 0
	self._gNoitceTitle = ""
	self._gNoitceContent = ""
	self._gLeaderName = ""
	self._gApplyMode = 0

	self._gFlagBaseIconID = ""
	self._gFlagAdditionalIconId = ""
	self._gFlagBaseIconColorId = 0
	self._gFlagAdditionalIconColorId = 0

	self._gFund = 0
	self._gOnlineMemberNum = 0

	self._gAlreadyApply = false

	self._description = ""

	self._maxMemberLimit = 0 -- 公会人数上限
	self._upperMemberLimitTimes = 0 -- 公会人数扩充次数

	self._residualSkillPoints = 0 --公会剩余技能点数
	self._talentInfos = {} --公会技能信息

	--公会领地信息
	self._territoryLv = 0 --领地等级
	self._territoryExp = 0 --当前领地等级下的经验 而非所有经验
	self._ownTownsIds = {} --拥有的城镇id集
	self._maxProsperity = 0 --拥有的繁荣度上限
	self._activityTerritoryInfo = {} --已激活的爵位信息 包括爵位id和激活时间 爵位ID为key 激活时间为值 方便查找
	self._conferTitleInfo = {} --已授予官职信息 包括 官职id和数量 id为KEY 数量为值
	self._maxActiveTerritoryId = 0 --最大激活的爵位ID
	self._changeLeadStamp = 0 --会长转让时间戳
	self._todayAddExp = 0 --今日增加的经验值(公会每日有经验获取上限(不包含公会技能奖励的经验)，公会每日获得的经验不能超过公会当前的最大人数)
	self._autoApplyLvLimit = 0 --公会申请自动审批--等级门槛
	self._autoApplyCombatPowerLimit = 0 --公会申请自动审批--战力门槛

	self._totalCombatPower = 0 --总战力
	self._highestCombatPowr = 0 --最高战力
	self._totalContribution = 0 --总贡献度
	self._totalProsperity = 0 --总繁荣度
	self._activeMemberNum = 0 --活跃人数

	if data then
		self:read(data)
	end
end

function ClassRef:read(data)
	self._gid = data.gid
	self._gName = data.name
	self._gLv = data.lv
	self._gExp = data.exp
	self._gMemberNum = data.memberNum
	self._gNoitceTitle = data.noticeTitle
	self._gNoitceContent = data.noticeContent
	self._gLeaderName = data.leaderName
	self._gApplyMode = data.applyMode

	self._gFlagBaseIconID = data.flagBaseIcon
	self._gFlagAdditionalIconId = data.flagAdditionalIcon
	self._gFlagBaseIconColorId = data.flagBaseIconColor
	self._gFlagAdditionalIconColorId = data.flagAdditionalIconColor

	self._gFund = data.fund
	self._gOnlineMemberNum = data.onlineMemberNum

	self._gAlreadyApply = data.alreadyApply
	self._description = data.description

	self._changeLeadStamp = data.changeLeaderStamp --会长转让时间戳
	self._todayAddExp = data.todayAddExp --今日增加的经验值

	self._autoApplyLvLimit = data.autoApplyLvLimit --公会申请自动审批--等级门槛
	self._autoApplyCombatPowerLimit = data.autoApplyCombatPowerLimit --公会申请自动审批--战力门槛

	self._maxMemberLimit = data.maxMemberLimit -- 公会人数上限
	self._upperMemberLimitTimes = data.upperMemberLimitTimes -- 公会人数扩充次数
	self._residualSkillPoints = data.residualSkillPoints or 0 --公会剩余技能点


	self._totalCombatPower = data.totalCombatPower --总战力
	self._highestCombatPowr = data.highestCombatPowr --最高战力
	self._totalContribution = data.totalContribution --总贡献度
	self._totalProsperity = data.totalProsperity --总繁荣度
	self._activeMemberNum = data.activeMemberNum --活跃人数

	-- msdk 公会绑群
	local msdkGroupId = rawget(data, "msdkGroupId")
	if msdkGroupId then
		self:setMsdkGroupId(msdkGroupId)
	else
		self:setMsdkGroupId("")
	end
	


	--talentInfo {skillId = donateLv}
	self._talentInfos = {}
	if data.skillInfos then
		for _,v in ipairs(data.skillInfos) do
			self._talentInfos[v.id] = v.donateLv or 0
		end
	end

	--公会领地信息
	--dump(data.territoryInfo)
	self:updateTerritoryData(data.territoryInfo)
end

function ClassRef:updateTerritoryData(territoryInfo)
	if territoryInfo then
		self._territoryLv = territoryInfo.lv --领地等级
		self._territoryExp = territoryInfo.exp --当前领地等级下的经验 而非所有经验
		self._ownTownsIds = territoryInfo.townIds --拥有的城镇id集
		self._maxProsperity = 0
		for k,v in ipairs(self._ownTownsIds) do
			local cityInfo = GD:getDistrictInfoById(v)
			if cityInfo then
				self._maxProsperity = self._maxProsperity + checknumber(cityInfo.prosperitySum)
			end
		end


		self._activityTerritoryInfo = {}
		self._maxActiveTerritoryId = 0
		for k,v in ipairs(territoryInfo.titleInfos) do
			self._activityTerritoryInfo[v.id] = v.activeTime
			if self._maxActiveTerritoryId < v.id then
				self._maxActiveTerritoryId = v.id
			end
		end
		self._conferTitleInfo = {} --已授予官职信息 包括 官职id和数量 id为KEY 数量为值
		for k,v in ipairs(territoryInfo.conferInfos) do
			self._conferTitleInfo[v.titleId] = v.num
		end
	end
end

--获得已激活爵位时间
function ClassRef:getActiveTerritoryTime(id)
	return self._activityTerritoryInfo[id]
end

--获得已授予的官职数量
function ClassRef:getConferTitle(titleId)
	return self._conferTitleInfo[titleId] or 0
end

--获得已激活的最大的爵位id
function ClassRef:getMaxActiveTerritoryId()
	return self._maxActiveTerritoryId
end

function ClassRef:getCurTerritoryNeedExp()
	--这里显示需要等级应该显示更高一级爵位需要的繁荣度
	local nextId = self._maxActiveTerritoryId
	local allforceInfos = GD:queryGuildForceInfos()
	local needExp = 0
	for _,v in ipairs(allforceInfos) do
		if nextId == tonumber(v.peerageID) then
			needExp = tonumber(v.totalProsperity)
			break
		end
	end
	return needExp
end

function ClassRef:getCurTerritoryOwnExp()
	return self._territoryExp
end

--所拥有的势力城镇
function ClassRef:getOwnTownList()
	return self._ownTownsIds
end

--公会拥有的城镇总数
function ClassRef:getOwnTownNum()
	if not self._ownTownsIds then
		return 0
	end
	return #self._ownTownsIds
end

-- 返回公会Id
function ClassRef:getGuildId()
	return self._gid
end
-- 返回公会名字
function ClassRef:getGuildName()
	return self._gName
end

-- 返回公会总战力
function ClassRef:getGuildTotalCombatPw()
	return self._totalCombatPower
end

--返回公会最高战力
function ClassRef:getHighestCombatPowr()
	return self._highestCombatPowr
end

--返回总贡献度
function ClassRef:getTotalContribution()
	return self._totalContribution
end

--返回总繁荣度
function ClassRef:getTotalProsperity()
	return self._totalProsperity
end

--返回总的繁荣度上限
function ClassRef:getMaxProsperity()
	return self._maxProsperity
end

--活跃人数
function ClassRef:getActiveMemberNum()
	return self._activeMemberNum
end

-- 返回公会等级
function ClassRef:getGuildLv()
	return self._gLv
end
-- 返回公会资金
function ClassRef:getGuildFund()
	return self._gFund
end
-- 返回公会经验值
function ClassRef:getGuildExp()
	return self._gExp
end
-- 返回会长名字
function ClassRef:getGuildLeaderName()
	return self._gLeaderName
end
-- 返回公会成员人数
function ClassRef:getGuildMemberNum()
	return self._gMemberNum
end
-- 返回公会在线成员人数
function ClassRef:getGuildOnlineMemberNum()
	return self._gOnlineMemberNum
end

-- 返回公会人数上限
function ClassRef:getGuildMaxMemberNum()
	return self._maxMemberLimit
end

--返回公会可用技能点
function ClassRef:getGuildLastSkillPoint()
	return self._residualSkillPoints
end
--返回公会的激活技能信息 {id:lv ...}
function ClassRef:getGuildCurTalentInfo()
	return self._talentInfos
end
-- 返回公会扩充过的次数
-- function ClassRef:getGuildUpedNum()
-- 	return self._upperMemberLimitTimes
-- end

-- 返回公会 布告/公告 标题
function ClassRef:getGuildNoticeTitle()
	return self._gNoitceTitle
end
-- 返回公会 布告/公告 内容
function ClassRef:getGuildNoticeContent()
	return self._gNoitceContent
end
-- 返回公会desc 宣言？
function ClassRef:getGuildDesc()
	return self._description
end
-- 公会入会是否需要审批 (0:需要审批 1.自动审批 默认为0)
function ClassRef:needAuditing()
	return self._gApplyMode == 0
end

function ClassRef:autoApplyLvLimit()
	return self._autoApplyLvLimit
end

function ClassRef:autoApplyCombatPowerLimit()
	return self._autoApplyCombatPowerLimit
end

-- 我是否申请过这个公会
function ClassRef:hadApplied()
	return self._gAlreadyApply
end

function ClassRef:setApplied(b)
 	self._gAlreadyApply = b
end

-- 返回公会头像数据结构
function ClassRef:getGuildIcon()
	local param = {
		baseIconId = self._gFlagBaseIconID,
		baseIconColor = self._gFlagBaseIconColorId,
		additionIconId = self._gFlagAdditionalIconId,
		additionIconColor = self._gFlagAdditionalIconColorId
	}
	return param
end

function ClassRef:isTodayAddExpEnough()
	return self._todayAddExp >= self._maxMemberLimit
end

function ClassRef:getTodayAddExp()
	return self._todayAddExp
end

function ClassRef:getMaxMemberLimit()
	return self._maxMemberLimit
end

--公会会长是否可以转让
function ClassRef:canChangeLead()
	if self._changeLeadStamp == 0 then
		return true
	end

	local serverTime = app.session:getServerTime()
	local detalTime = serverTime - self._changeLeadStamp
	--判断detalTime是否大于CD
	local cdTime = GD:getSystemPar( "guildPosition1CD",24 ) --公会会长转让CD 小时为单位
	return detalTime >= cdTime * 3600
end

--
function ClassRef:getMsdkGroupId()
	return self._msdkGroupId
end
function ClassRef:setMsdkGroupId(id)
	local oldMsdkGroupId = self._msdkGroupId
	self._msdkGroupId = id
	-- 绑群id 发生变化
	if (oldMsdkGroupId and oldMsdkGroupId ~= "" and id == "") or (oldMsdkGroupId == "" and id and id ~= "") then
		-- msdk group id change
		print("===================>>>>>>>>>> queryGroupInfo")
		UD.msdkData:queryGroupInfo()
	end
end
return ClassRef
